﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

namespace MGP_004CompoundBigWatermelon
{
    /// <summary>
    ///  UIManager 脚本，主要的功能是：实时显示分数，游戏结束的时候显示结束面板，并且添加重新开始游戏的按钮事件，重新加载场景，重新开始游戏
    /// </summary>
    public class UIManager : IManager
    {
        private Text m_ScoreText;
        private GameObject m_GameOverImageGo;
        private Button m_RestartGameButton;

        private ScoreManager m_ScoreManager;
        /// <summary>
        /// 在 Init 方法中，接受了两个 Transform 类型的参数 worldTrans 和 uiTrans。
        /// 这两个参数分别代表了场景中的世界坐标系中的 Transform 和 UI 坐标系中的 Transform。
        /// </summary>
        /// <param name="worldTrans"></param>
        /// <param name="uiTrans"></param>
        /// <param name="manager"></param>
        public void Init(Transform worldTrans, Transform uiTrans, params object[] manager)
        {
            m_ScoreText = uiTrans.Find(GameObjectPathInSceneDefine.UI_SCORE_TEXT_PATH).GetComponent<Text>();
            ///拿到场景游戏物体
            m_GameOverImageGo = uiTrans.Find(GameObjectPathInSceneDefine.UI_GAME_OVER_IMAGE_PATH).gameObject;
            m_RestartGameButton = uiTrans.Find(GameObjectPathInSceneDefine.UI_RESTART_GAME_BUTTON_PATH).GetComponent<Button>();

            m_ScoreManager = manager[0] as ScoreManager;
            ///打不打√
            m_GameOverImageGo.SetActive(false);
            m_ScoreText.text = m_ScoreManager.Score.ToString();
            m_ScoreManager.OnValueChanged += OnScroeValueChanged;
            m_RestartGameButton.onClick.AddListener(OnRestartButton);
        }

        public void Update()
        {

        }

        public void Destroy()
        {
            m_ScoreManager.OnValueChanged -= OnScroeValueChanged;
            m_RestartGameButton.onClick.RemoveAllListeners();

            m_ScoreText = null;
            m_GameOverImageGo = null;
            m_RestartGameButton = null;
            m_ScoreManager = null;
        }
        /// <summary>
        /// OnGameOver() 游戏结束时，显示结束面板
        /// </summary>
        public void OnGameOver()
        {
            m_GameOverImageGo.SetActive(true);
        }
        /// <summary>
        /// OnScroeValueChanged(int score) 更新游戏得分
        /// </summary>
        private void OnScroeValueChanged(int score)
        {
            m_ScoreText.text = score.ToString();
        }
        /// <summary>
        /// OnRestartButton()游戏重新开始,卧槽，为什么unity这么多自带的api
        /// </summary>
        private void OnRestartButton()
        {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }
    }
}
